/**
 *
 */
import Util;
import OpenApi;
import OpenApiUtil;
import EndpointUtil;

extends OpenApi;


init(config: OpenApi.Config){
  super(config);
  @endpointRule = '';
  
  checkConfig(config);
  @endpoint = getEndpoint('tunnel-service', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }
  
  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model CreateSessionRequest {
  sessionName?: string(name='sessionName'),
}

model CreateSessionResponseBody = {
  code?: string(name='code', description='ErrorCode'),
  data?: {
    localInstanceId?: string(name='localInstanceId', description='本地实例ID'),
    remoteInstanceId?: string(name='remoteInstanceId', description='远端实例ID'),
    sessionId?: string(name='sessionId', description='Session ID'),
    sessionName?: string(name='sessionName', description='Session名字'),
  }(name='data'),
  message?: string(name='message', description='错误信息'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model CreateSessionResponse = {
  headers: map[string]string(name='headers'),
  body: CreateSessionResponseBody(name='body'),
}

async function createSession(request: CreateSessionRequest): CreateSessionResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return createSessionWithOptions(request, headers, runtime);
}

async function createSessionWithOptions(request: CreateSessionRequest, headers: map[string]string, runtime: Util.RuntimeOptions): CreateSessionResponse {
  Util.validateModel(request);
  var body : map[string]any= {};
  if (!Util.isUnset(request.sessionName)) {
    body['sessionName'] = request.sessionName;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'CreateSession',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/sessions/`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model DeleteSessionResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    sessionId?: string(name='sessionId', description='session ID'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model DeleteSessionResponse = {
  headers: map[string]string(name='headers'),
  body: DeleteSessionResponseBody(name='body'),
}

async function deleteSession(sessionId: string): DeleteSessionResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return deleteSessionWithOptions(sessionId, headers, runtime);
}

async function deleteSessionWithOptions(sessionId: string, headers: map[string]string, runtime: Util.RuntimeOptions): DeleteSessionResponse {
  sessionId = OpenApiUtil.getEncodeParam(sessionId);
  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
  };
  var params = new OpenApi.Params{
    action = 'DeleteSession',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/sessions/${sessionId}`,
    method = 'DELETE',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model GetInstanceResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    desc?: string(name='desc', description='描述说明'),
    instanceId?: string(name='instanceId', description='实例 ID'),
    params?: string(name='params', description='自定义参数'),
    proxyIp?: string(name='proxyIp', description='代理实例 IP'),
    proxyPrivateKey?: string(name='proxyPrivateKey', description='代理实例私钥'),
    proxyRemotePort?: string(name='proxyRemotePort', description='代理实例端口'),
    remotePrivateKey?: string(name='remotePrivateKey', description='远端实例私钥'),
    remotePubKey?: string(name='remotePubKey', description='远端实例公钥'),
    sessionId?: string(name='sessionId', description='session ID'),
    source?: string(name='source', description='来源'),
    status?: string(name='status', description='session状态'),
    type?: string(name='type', description='实例类型'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model GetInstanceResponse = {
  headers: map[string]string(name='headers'),
  body: GetInstanceResponseBody(name='body'),
}

async function getInstance(instanceId: string): GetInstanceResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return getInstanceWithOptions(instanceId, headers, runtime);
}

async function getInstanceWithOptions(instanceId: string, headers: map[string]string, runtime: Util.RuntimeOptions): GetInstanceResponse {
  instanceId = OpenApiUtil.getEncodeParam(instanceId);
  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
  };
  var params = new OpenApi.Params{
    action = 'GetInstance',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/instances/${instanceId}`,
    method = 'GET',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model GetSessionResponseBody = {
  code?: string(name='code', description='ErrorCode'),
  data?: {
    localInstanceId?: string(name='localInstanceId', description='本地实例ID'),
    proxyId?: string(name='proxyId', description='代理实例ID'),
    proxyIp?: string(name='proxyIp', description='代理实例公网IP'),
    proxyRemotePort?: string(name='proxyRemotePort', description='代理实例端口'),
    remoteInstanceId?: string(name='remoteInstanceId', description='远端实例ID'),
    sessionId?: string(name='sessionId', description='session ID'),
    status?: string(name='status', description='session 状态'),
  }(name='data'),
  message?: string(name='message', description='错误信息'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model GetSessionResponse = {
  headers: map[string]string(name='headers'),
  body: GetSessionResponseBody(name='body'),
}

async function getSession(sessionId: string): GetSessionResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return getSessionWithOptions(sessionId, headers, runtime);
}

async function getSessionWithOptions(sessionId: string, headers: map[string]string, runtime: Util.RuntimeOptions): GetSessionResponse {
  sessionId = OpenApiUtil.getEncodeParam(sessionId);
  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
  };
  var params = new OpenApi.Params{
    action = 'GetSession',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/sessions/${sessionId}`,
    method = 'GET',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model HeartBeatRequest {
  instanceId?: string(name='instanceId'),
  instanceType?: string(name='instanceType'),
  sessionStatus?: string(name='sessionStatus'),
}

model HeartBeatResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    sessionStatus?: string(name='sessionStatus', description='session 状态'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model HeartBeatResponse = {
  headers: map[string]string(name='headers'),
  body: HeartBeatResponseBody(name='body'),
}

async function heartBeat(request: HeartBeatRequest): HeartBeatResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return heartBeatWithOptions(request, headers, runtime);
}

async function heartBeatWithOptions(request: HeartBeatRequest, headers: map[string]string, runtime: Util.RuntimeOptions): HeartBeatResponse {
  Util.validateModel(request);
  var body : map[string]any= {};
  if (!Util.isUnset(request.instanceId)) {
    body['instanceId'] = request.instanceId;
  }
  if (!Util.isUnset(request.instanceType)) {
    body['instanceType'] = request.instanceType;
  }
  if (!Util.isUnset(request.sessionStatus)) {
    body['sessionStatus'] = request.sessionStatus;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'HeartBeat',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/sessions/`,
    method = 'PUT',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model ListSessionsRequest {
  pageNum?: int32(name='pageNum'),
  pageSize?: int32(name='pageSize'),
}

model ListSessionsResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    hasNextPage?: boolean(name='hasNextPage', description='是否有下一页'),
    nextPageNum?: int32(name='nextPageNum', description='下一页'),
    nextPageSize?: int32(name='nextPageSize', description='下一页的session个数限制'),
    sessions?: [ 
      {
        localInstanceId?: string(name='localInstanceId', description='本地实例ID'),
        proxyId?: string(name='proxyId', description='代理实例ID'),
        proxyIp?: string(name='proxyIp', description='代理实例公网IP'),
        proxyRemotePort?: string(name='proxyRemotePort', description='代理实例端口'),
        remoteInstanceId?: string(name='remoteInstanceId', description='远端实例ID'),
        sessionId?: string(name='sessionId', description='session ID'),
        sessionName?: string(name='sessionName', description='session 名字'),
        status?: string(name='status', description='session 状态'),
      }
    ](name='sessions'),
    total?: int32(name='total', description='session 总个数'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model ListSessionsResponse = {
  headers: map[string]string(name='headers'),
  body: ListSessionsResponseBody(name='body'),
}

async function listSessions(request: ListSessionsRequest): ListSessionsResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return listSessionsWithOptions(request, headers, runtime);
}

async function listSessionsWithOptions(request: ListSessionsRequest, headers: map[string]string, runtime: Util.RuntimeOptions): ListSessionsResponse {
  Util.validateModel(request);
  var query : map[string]any= {};
  if (!Util.isUnset(request.pageNum)) {
    query['pageNum'] = request.pageNum;
  }
  if (!Util.isUnset(request.pageSize)) {
    query['pageSize'] = request.pageSize;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    query = OpenApiUtil.query(query),
  };
  var params = new OpenApi.Params{
    action = 'ListSessions',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/sessions/`,
    method = 'GET',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model RegisterInstanceRequest {
  description?: string(name='description'),
  instanceId?: string(name='instanceId'),
  instanceType?: string(name='instanceType'),
  params?: string(name='params'),
  sessionId?: string(name='sessionId'),
  source?: string(name='source'),
}

model RegisterInstanceResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    instanceId?: string(name='instanceId', description='实例ID'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model RegisterInstanceResponse = {
  headers: map[string]string(name='headers'),
  body: RegisterInstanceResponseBody(name='body'),
}

async function registerInstance(request: RegisterInstanceRequest): RegisterInstanceResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return registerInstanceWithOptions(request, headers, runtime);
}

async function registerInstanceWithOptions(request: RegisterInstanceRequest, headers: map[string]string, runtime: Util.RuntimeOptions): RegisterInstanceResponse {
  Util.validateModel(request);
  var body : map[string]any= {};
  if (!Util.isUnset(request.description)) {
    body['description'] = request.description;
  }
  if (!Util.isUnset(request.instanceId)) {
    body['instanceId'] = request.instanceId;
  }
  if (!Util.isUnset(request.instanceType)) {
    body['instanceType'] = request.instanceType;
  }
  if (!Util.isUnset(request.params)) {
    body['params'] = request.params;
  }
  if (!Util.isUnset(request.sessionId)) {
    body['sessionId'] = request.sessionId;
  }
  if (!Util.isUnset(request.source)) {
    body['source'] = request.source;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'RegisterInstance',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/instances/`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

model UnRegisterInstanceResponseBody = {
  code?: string(name='code', description='错误码'),
  data?: {
    instanceId?: string(name='instanceId', description='实例ID'),
  }(name='data'),
  message?: string(name='message', description='错误详情'),
  requestId?: string(name='requestId', description='请求ID'),
  success?: boolean(name='success', description='是否成功'),
}

model UnRegisterInstanceResponse = {
  headers: map[string]string(name='headers'),
  body: UnRegisterInstanceResponseBody(name='body'),
}

async function unRegisterInstance(instanceId: string): UnRegisterInstanceResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return unRegisterInstanceWithOptions(instanceId, headers, runtime);
}

async function unRegisterInstanceWithOptions(instanceId: string, headers: map[string]string, runtime: Util.RuntimeOptions): UnRegisterInstanceResponse {
  instanceId = OpenApiUtil.getEncodeParam(instanceId);
  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
  };
  var params = new OpenApi.Params{
    action = 'UnRegisterInstance',
    version = '2021-05-09',
    protocol = 'HTTPS',
    pathname = `/v1/instances/${instanceId}`,
    method = 'PUT',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

